package qs.openxt.libs.util;

import java.io.File;
import java.util.Random;
import java.util.UUID;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;

import qs.openxt.libs.App;

public class Utils {
    public static final String PREFS_NAME = "OPENXT_MESSAGE";
    
    public static final String TAGS = "TAGS";

    // 获取ApiKey
    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                apiKey = metaData.getString(metaKey);
            }
        } catch (Exception e) {

        }
        return apiKey;
    }
    
    public static void setLocalStorage(Context mContent, String key, String value){
        if(mContent == null)
            return;
        SharedPreferences settings = mContent.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        editor.apply();
	}

    public static void removeLocalStorage(Context mContent, String key){
        if(mContent == null)
            return;
        SharedPreferences settings = mContent.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.remove(key);
        editor.apply();
    }

    public static SharedPreferences getSharedPreferences(Context mContent){
        if(mContent == null)
            return null;
        SharedPreferences settings = mContent.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
        return settings;
    }
	
    public static String getLocalStorage(Context mContent, String key){
        if(mContent == null)
            return null;
        SharedPreferences settings = mContent.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
        return settings.getString(key, null);
	}
    
    public static int getUUID(){
    	int[] array = {0,1,2,3,4,5,6,7,8,9};
    	Random rand = new Random();
    	for (int i = 10; i > 1; i--) {
    	    int index = rand.nextInt(i);
    	    int tmp = array[index];
    	    array[index] = array[i - 1];
    	    array[i - 1] = tmp;
    	}
    	int result = 0;
    	for(int i = 0; i < 7; i++)
    	    result = result * 10 + array[i];
    	return Integer.valueOf(result);
    }

    public static String getExternalStorageDirectory(){
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    public static File getFile(final String suffix) {
		File f = new File(Environment.getExternalStorageDirectory(), "/" + App.PATH_NAME + "/record/" + UUID.randomUUID().toString().substring(24) + "." + suffix);
		if (!f.getParentFile().exists())
			f.getParentFile().mkdirs();
		return f;
	}
}
